In this tutorial, we will discuss how can we create and import python modules and learn how and where to use modules in python.
Python Modules
What are the modules?
Modules are the python files or script which contain python code or statement and definition Suppose you write some python code on your notepad and save the file by the name main.py. The file will become the module, and the main will be the module name. Modules are used to break a complete program into smaller parts, and later on we can use them at once using the import keyword. Suppose that you have a team of 5 people, and your project is to create a desktop application so you can divide the different projects into modules among five people and at last with the help of import keyword you can use all the modules.
How to import a module?
We use the import keyword to import a module from one file to another module. For example, create a python file use.py and if you want to import the functions and objects of this file just use the keyword import and the file name use.
Let’s understand it with an example:
################################## use.py file#######################
def mul(num1,num2):
return num1*num2
##################################### main.py file ######################
import use
print(use.mul(3,6))
hjjhxc
#output of main.py
18
Behind the code:
In the above example, we create 2 modules use.py and main.py, and with the help of import keyword we import the properties of use . py file and use it in main.py file.
Python import Statement
In python, there are many inbuilt modules that we often use to add more functionality to the program. All these in-built modules are also known as python standard modules , basically, these are the files present in the Lib directory where your python is installed.
Let’s import a standard module into the program:
import math
val = 25
sqt = math.sqrt(val)
print(sqt)
#Output
5.0
Behind the code
In the above code first, we import the math module in our current program and use a function sqrt() which defined in the math module which returns the square root value of the number passed along the sqrt() function. You can see that when we are accessing the sqrt() function we are using module name math. along with it, because everything in python is an object so here math is an object, and sqrt () is its property. You will learn more about objects and property in the Python Class article .
Renaming the import module with a keyword
In the above example, we use the math module and whenever we call any function of math , we use math as an object to call its function. As in the previous articles, we have learned that with the Assignment operator (=) we can assign names to the objects, but when it’s come to assign a name to the import modules, we use the keyword as . The as a keyword, it not just only gives a name to the module rather renames the module.
Let’s understand it with an example: Same as above example
import math as m # renaming the math module
val = 25
sq = m.sqrt(val)
print(sq)
#Output
5.0
Python from….import statement:
If a module contains many functions or definitions and you want to import only some specific functions out of them, then we use a combination of from and import statements.
Syntax of from and import:
from module_name import module_properties
Let’s understand it with an example:
from math import pow
p = pow(3,4) # pow() function return the 34
print(p)
#Output
81.0
Behind the code:
The above from and import statement meaning it imports only one function pow () from the module math . If you want to import all the property of a module, we use the asterisks mark (*) along with the import
from math import *
p = pow(3,4)
sqt = sqrt(80)
pie = pi
print(p)
print(sqt)
print(pie)
#Output
81.0
8.94427190999916
3.141592653589793
Python Module Search path:
When you import a module the python interpreter search for the module in different directories of your system. At first search, it looks for the built-in or standard modules and then into those directories, which are defined in the sys.path list. To check your sys.path list open your command prompt or terminal and enter python or py. Then write this code:
>>> import sys
>>> sys.path
['',
'C:\\Python33\\Lib\\idlelib',
'C:\\Windows\\system32\\python33.zip',
'C:\\Python33\\DLLs',
'C:\\Python33\\lib',
'C:\\Python33',
'C:\\Python33\\lib\\site-packages']
If you want, you can modify this list and add more paths.
Python dir() Built-in function:
dir() is a built-in function in python which is used to find out all the name defined inside a module. For example, let’s check all the names defined inside the math module:
import math
print(dir(math))
#Output
['__doc__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'pi', 'pow', 'radians', 'remainder', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc']